//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.OrderSet;
import com.aidong.dao.wills.BaseDao;
import com.aidong.model.wills.DicShopCode;
import com.aidong.model.wills.PtCoachCode;
import java.util.Iterator;
import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

@Repository
public class PtCoachCodeDao extends BaseDao<PtCoachCode, String> {
	public PtCoachCodeDao() {
	}

	public PtCoachCode getById(String code) {
		return this.getByCode(code);
	}

	public PtCoachCode getByCode(String code) {
		return (PtCoachCode)this.getByWhere("code=:p1", new Object[]{code});
	}

	public PtCoachCode getByName(String name) {
		return (PtCoachCode)this.getByWhere("chName=:p1", new Object[]{name});
	}

	public List<PtCoachCode> getShopCoachList(String shopCode) {
		String sql = "select code, chName, coachLevel,coachType,status\nfrom PtCoachCode c inner join PtCoachShop s on c.code=s.coachCode\nwhere shopCode=:p1 and status=1";
		return this.queryListBySql(PtCoachCode.class, sql, new Object[]{shopCode});
	}

	public List<PtCoachCode> getCoachListByType(String coachType) {
		String sqlww = "coachType=:p1 and status=1";
		return this.queryList(sqlww, new Object[]{coachType}, (OrderSet)null, -1);
	}

	public List<PtCoachCode> getListByMemberShop(String memberCode, String shopCode) {
		String sql = "select distinct cc.code,chName,coachLevel,coachType,cc.status\nfrom PtCoachCode cc inner join PtContract pc on cc.code=pc.coachCode\ninner join PtCoachShop s on cc.code=s.coachCode\nwhere memberCode=:p1 and s.shopCode=:p2";
		return this.queryListBySql(PtCoachCode.class, sql, new Object[]{memberCode, shopCode});
	}

	public List<PtCoachCode> getListByMemberCode(String memberCode) {
		String sql = "select distinct cc.code,chName,coachLevel,coachType,cc.status\nfrom PtCoachCode cc inner join PtContract pc on cc.code=pc.coachCode\nwhere memberCode=:p1 and quantity>bespeakQuantity";
		return this.queryListBySql(PtCoachCode.class, sql, new Object[]{memberCode});
	}

	public boolean updateStatus(String code, int status) {
		String hql = "update PtCoachCode set status=:p1 where code=:p2";
		return this.updateByHql(hql, new Object[]{Integer.valueOf(status), code}) > 0;
	}

	public List<DicShopCode> getCoachShopList(String coachCode, boolean ball) {
		String sql;
		if(ball) {
			sql = "select s.code, s.chName, (case when a.shopCode is null then 0 else 1 end) status from DicShopCode s\nleft join PtCoachShop a on s.code=a.shopCode and coachCode = :p1\nwhere status>0 order by s.code";
		} else {
			sql = "select s.code, s.chName, status from DicShopCode s\ninner join PtCoachShop a on s.code=a.shopCode\nwhere coachCode = :p1 and status>0 order by s.code";
		}

		return this.queryListBySql(DicShopCode.class, sql, new Object[]{coachCode});
	}

	public void updateCoachShop(String coachCode, List<String> shopList) {
		Session session = this.getSession();
		SQLQuery query = session.createSQLQuery("delete from PtCoachShop where coachCode = :p1");
		query.setString("p1", coachCode);
		query.executeUpdate();
		query = session.createSQLQuery("insert into PtCoachShop (coachCode, ShopCode) values (:p1, :p2)");
		query.setString("p1", coachCode);
		Iterator i$ = shopList.iterator();

		while(i$.hasNext()) {
			String shopCode = (String)i$.next();
			query.setString("p2", shopCode);
			query.executeUpdate();
		}

	}
}
